package com.example.jiaju.service.impl;

import com.example.jiaju.dto.HostNameDto;
import com.example.jiaju.enums.ResultEnum;
import com.example.jiaju.enums.UserHostIsUseEnum;
import com.example.jiaju.enums.UserHostRoleEnum;
import com.example.jiaju.exception.SMException;
import com.example.jiaju.dto.UserNameDto;
import com.example.jiaju.dataobject.*;
import com.example.jiaju.repository.*;
import com.example.jiaju.service.UserHostService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;


@Service
public class UserHostServiceImpl implements UserHostService {

    @Autowired
    private UserHostRepository userHostRepository;

    @Autowired
    private HostRepository hostRepository;

    @Autowired
    private UserRepository userRepository;

    @Override
    @Transactional
    public List<HostNameDto> findHostList(String phone){

        //查询用户所有主机
        List<UserHost> UUserHostList =userHostRepository.findByUserId(phone);
        if(CollectionUtils.isEmpty(UUserHostList)){
            throw new SMException(ResultEnum.USER_NOT_FOUND);
        }
        List<HostNameDto> hostNameDtoList=new ArrayList<>();
        for(UserHost userHost : UUserHostList){

            //查询主机名称
            Host host=hostRepository.findByIEEE(userHost.getHostId());
            if(host==null){
                throw new SMException(ResultEnum.HOST_NOT_FOUND);
            }

            //拼装
            HostNameDto hostNameDto=new HostNameDto();
            hostNameDto.setIEEE(userHost.getHostId());
            hostNameDto.setName(host.getName());
            hostNameDto.setIsUse(userHost.getIsUse());
            hostNameDto.setRole(userHost.getRole());


            hostNameDtoList.add(hostNameDto);
        }

        return hostNameDtoList;
    }

    @Override
    public UserHost findByUserIdAndHostId(String phone, String IEEE) {
        return userHostRepository.findByUserIdAndHostId(phone,IEEE);
    }

    //绑定主机
    @Override
    @Transactional
    public UserHost bindHost(String phone, String IEEE) {
        User user=userRepository.findByPhone(phone);
        if (user==null){
            throw new SMException(ResultEnum.USER_NOT_FOUND);
        }

        if(userHostRepository.findByUserIdAndHostId(phone,IEEE)!=null){
            throw new SMException(ResultEnum.USER_HOST_IS_BIND);
        }

        //切换当前使用主机
        List<UserHost> userHostList=userHostRepository.findByUserId(phone);
        for(UserHost userHost1 : userHostList){
            userHost1.setIsUse(UserHostIsUseEnum.NO_UES.getCode());
            userHostRepository.save(userHost1);
        }

        UserHost userHost=new UserHost();
        userHost.setUserId(phone);
        userHost.setHostId(IEEE);


        if(hostRepository.findByIEEE(IEEE)==null){
            Host host=new Host();
            host.setIEEE(IEEE);
            host.setName(IEEE);
            hostRepository.save(host);
            userHost.setRole(UserHostRoleEnum.ADMIN.getCode());
        }
        userHostRepository.save(userHost);
        return userHost;
    }

    //切换主机
    @Override
    @Transactional
    public UserHost switchHost(String phone, String IEEE) {

        //查询用户当前主机
        UserHost UserHost = userHostRepository.findByUserIdAndHostId(phone,IEEE);
        if (UserHost ==null){
            throw new SMException(ResultEnum.USER_HOST_NOT_FOUND);
        }
        //判断使用状态
        if(!UserHost.getIsUse().equals(UserHostIsUseEnum.NO_UES.getCode())){
            throw new SMException(ResultEnum.IS_USE_ERROR);
        }

        //切换主机
        List<UserHost> UserHostList = userHostRepository.findByUserId(phone);
        for(UserHost UserHost1 : UserHostList){
            UserHost1.setIsUse(UserHostIsUseEnum.NO_UES.getCode());
            userHostRepository.save(UserHost1);
        }
        UserHost.setIsUse(UserHostIsUseEnum.IS_USE.getCode());
        userHostRepository.save(UserHost);
        return UserHost;
    }

    @Override
    public UserHost allocateRole(String allocateUser, String IEEE, String acceptUser ) {
        //验证是否为管理员
        UserHost admin= userHostRepository.findByUserIdAndHostId(allocateUser,IEEE);
        if (!admin.getRole().equals(UserHostRoleEnum.ADMIN.getCode())){
            throw new SMException(ResultEnum.NOT_ADMIN);
        }

        //查询用户当前主机
        UserHost userHost = userHostRepository.findByUserIdAndHostId(acceptUser,IEEE);
        if (userHost ==null){
            throw new SMException(ResultEnum.USER_HOST_NOT_FOUND);
        }

        //判断用户权限
        if(!userHost.getRole().equals(UserHostRoleEnum.NOT.getCode())){
            throw new SMException(ResultEnum.ROLE_ERROR);
        }

        userHost.setRole(UserHostRoleEnum.USER.getCode());
        userHostRepository.save(userHost);
        return userHost;
    }

    //转让权限
    @Override
    @Transactional
    public UserHost transferAdminRole(String transferUser, String IEEE, String acceptUser) {
        //验证是否为管理员
        UserHost admin= userHostRepository.findByUserIdAndHostId(transferUser,IEEE);
        if (!admin.getRole().equals(UserHostRoleEnum.ADMIN.getCode())){
            throw new SMException(ResultEnum.NOT_ADMIN);
        }

        //查询用户当前主机
        UserHost userHost = userHostRepository.findByUserIdAndHostId(acceptUser,IEEE);
        if (userHost ==null){
            throw new SMException(ResultEnum.USER_HOST_NOT_FOUND);
        }

        if(!userHost.getRole().equals(UserHostRoleEnum.USER.getCode())){
            throw new SMException(ResultEnum.ROLE_ERROR);
        }

        //转让权限
        userHost.setRole(UserHostRoleEnum.ADMIN.getCode());
        userHostRepository.save(userHost);
        admin.setRole(UserHostRoleEnum.USER.getCode());
        userHostRepository.save(admin);

        return userHost;
    }

    @Override
    public List<UserNameDto> findUserList(String phone,String IEEE) {
        //判断权限
        UserHost admin= userHostRepository.findByUserIdAndHostId(phone,IEEE);
        if(!admin.getRole().equals(UserHostRoleEnum.ADMIN.getCode())){
            throw new SMException(ResultEnum.NOT_ADMIN);
        }

        //查询
        List<UserHost> UserHostList = userHostRepository.getUsersByIEEE(IEEE);
        List<UserNameDto> userNameDtoList=new ArrayList<>();
        for(UserHost userHost : UserHostList){
            UserNameDto userNameDto=new UserNameDto();
            userNameDto.setPhone(userHost.getUserId());
            userNameDto.setRole(userHost.getRole());

            User User = userRepository.findByPhone(userHost.getUserId());
            userNameDto.setName(User.getName());

            userNameDtoList.add(userNameDto);
        }

        return userNameDtoList;
    }

    @Override
    public UserHost deleteUser(String adminUser, String IEEE, String phone) {
        //判断权限
        UserHost admin= userHostRepository.findByUserIdAndHostId(adminUser,IEEE);
        if(!admin.getRole().equals(UserHostRoleEnum.ADMIN.getCode())){
            throw new SMException(ResultEnum.NOT_ADMIN);
        }

        //查询用户当前主机
        UserHost UserHost = userHostRepository.findByUserIdAndHostId(phone,IEEE);
        if (UserHost ==null){
            throw new SMException(ResultEnum.USER_HOST_NOT_FOUND);
        }

        userHostRepository.delete(UserHost);

        return UserHost;
    }
}
